<script setup>
import { ref, computed, watch } from 'vue'
import axios from "axios";
import CryptoJS from "crypto-js"
import { backUrl } from "../../assets/js/setting.js";

const step = ref(0)
const accountNum = ref('')
const smsCode = ref('') //验证码
const hasSendSms = ref(false)
const password = ref() //密码
const password_twice = ref() //密码
const MsgBox = ref(false) //是否显示提示框
const BoxMsg = ref('')

axios.defaults.baseURL = backUrl;

//账号是否已输入
const hasAccount = computed(() => {
  if (accountNum.value === undefined) {
    return false
  } else if (accountNum.value.length === 0) {
    return false
  }
  return true
})
//密码是否已输入
const hasPwd = computed(() => {
  if (password.value === undefined) {
    return false
  } else if (password.value.length === 0) {
    return false
  }
  return true
})
const hasPwd2 = computed(() => {
  if (password_twice.value === undefined) {
    return false
  } else if (password_twice.value.length === 0) {
    return false
  }
  return true
})

watch(step, () => {
  MsgBox.value = false
  BoxMsg.value = ''
})

//确认账号是否存在
function clickfindPwdBtn1() {
  if (!hasAccount.value) {
    MsgBox.value = true
    BoxMsg.value = '请输入邮箱'
  }
  BoxMsg.value = ''
  //登录校验
  axios({
    url: '/BuildMind/user/existence',
    method: 'get',
    params: {
      email: accountNum.value
    },
  }).then((response) => {
    MsgBox.value = true
    if (response.status !== 200) {
      BoxMsg.value = response.data.message
    } else {
      BoxMsg.value = response.data.message
      step.value = 1
    }
  }).catch((error) => {
    console.log(error)
    if (error.response.data) {
      MsgBox.value = true
      BoxMsg.value = error.response.data.message
    }
    else if (error.request) {
      MsgBox.value = true
      BoxMsg.value = '网络错误'
    }
  })
}

function clickSmsBtn() {
  if (hasSendSms.value === true) {
    MsgBox.value = true;
    BoxMsg.value = '验证码已发送'
    return;
  } else {
    BoxMsg.value = ''
    smsCode.value = ''
  }
  //申请验证码
  axios({
    url: '/BuildMind/user/reset_password/get_verified',
    method: 'get',
    params: {
      email: accountNum.value
    },
  }).then((response) => {
    MsgBox.value = true
    if (response.status !== 200) {
      BoxMsg.value = response.data.message
    } else {
      BoxMsg.value = response.data.message
      hasSendSms.value = true
    }
  }).catch((error) => {
    console.log(error)
    if (error.response.data) {
      MsgBox.value = true
      BoxMsg.value = error.response.data.message
    }
    else if (error.request) {
      MsgBox.value = true
      BoxMsg.value = '网络错误'
    }
  })
}

function clickfindPwdBtn3() {
  if (smsCode.value.length !== 6) {
    MsgBox.value = true
    BoxMsg.value = '请输入验证码'
    return
  }
  if (!hasPwd.value) {
    MsgBox.value = true
    BoxMsg.value = '请输入密码'
    return
  } else if (!hasPwd2.value) {
    MsgBox.value = true
    BoxMsg.value = '请再次输入密码'
    return
  } else if (password.value !== password_twice.value) {
    MsgBox.value = true
    BoxMsg.value = '两次密码输入不一致'
    return
  }
  BoxMsg.value = ''
  //登录校验
  axios({
    url: '/BuildMind/user/reset_password',
    method: 'POST',
    data: {
      email: accountNum.value,
      verification_code: smsCode.value,
      password_hash: CryptoJS.SHA256(password.value).toString(),
    },
  }).then((response) => {
    MsgBox.value = true
    if (response.status !== 200) {
      BoxMsg.value = response.data.message
    } else {
      BoxMsg.value = response.data.message
      //存token
      location.href = "./BUILD-MIND";
    }
  }).catch((error) => {
    console.log(error)
    if (error.response.data) {
      MsgBox.value = true
      BoxMsg.value = error.response.data.message
    }
    else if (error.request) {
      MsgBox.value = true
      BoxMsg.value = '网络错误'
    }
  })
}

</script>

<template>
  <div class="findPwd-container">
    <el-steps :active="step" finish-status="success" align-center>
      <el-step title="输入账号" />
      <el-step title="重置密码" />
    </el-steps>

    <div class="findPwd-box">
      <div>重置密码</div>
      <div class="form-findPwd">
        <el-space direction="vertical" :size="16" style="width: 100%">
          <div class="first-findPwd-form" :style="{ 'display': (step === 0) ? 'block' : 'none' }">
            <!--账号输入框主体-->
            <form id="first-findPwd" method="post" onsubmit="return false;">
              <!--账号输入框-->
              <el-space direction="vertical">
                <el-input id="account-number" v-model="accountNum" class="form-input" placeholder="请输入邮箱" clearable />
                <!--提示信息-->
                <el-text class="msg-wrap" v-if="MsgBox" type="warning">
                  {{ BoxMsg }}
                </el-text>
                <!-- 下一步按钮 -->
                <el-button id="findPwd-submit1" class="submit-btn" style="width: 300px"
                  :class="{ 'findPwd-submit-box submit-btn-disable': !hasAccount }" :disabled="!hasAccount"
                  @click="clickfindPwdBtn1">下一步
                </el-button>
              </el-space>
            </form>
          </div>
          <div class="second-findPwd-form" :style="{ 'display': (step === 1) ? 'block' : 'none' }">
            <!--密码输入框主体-->
            <form id="second-findPwd" method="post" onsubmit="return false;">
              <el-space direction="vertical">
                <!--验证码输入框-->
                <el-input id="findPwd-sms-code" autocomplete="off" class="captcha-code form-input" maxlength="6"
                  name="smsCode" placeholder="请输入验证码" type="text" v-model="smsCode">
                  <template #append>
                    <el-button id="findPwd-send-sms-code-btn" :disabled="accountNum.length === 0" @click="clickSmsBtn">
                      获取验证码
                    </el-button>
                  </template>
                </el-input>
                <!--密码输入框-->
                <el-input id="pwd-number" placeholder="请输入密码" type="password" v-model="password" class="form-input"
                  clearable show-password />
                <!--密码二次输入框-->
                <el-input id="pwd-number" placeholder="请输入密码" type="password" v-model="password_twice"
                  class="form-input" clearable show-password />
                <!--提示信息-->
                <el-text class="msg-wrap" v-if="MsgBox" type="warning">
                  {{ BoxMsg }}
                </el-text>
                <!-- 提交按钮 -->
                <el-button id="findPwd-submit3" class="submit-btn" style="width: 300px" :disabled="!hasSendSms"
                  @click="clickfindPwdBtn3">下一步
                </el-button>
              </el-space>
            </form>
          </div>
        </el-space>
      </div>
    </div>
  </div>
</template>

<style scoped>
.form-input {
  width: 300px
}

/* 新增外层容器样式 */
.findPwd-container {
  width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 步骤条样式 */
:deep(.el-steps) {
  width: 100%;
  margin-bottom: 40px;
}

/* 找回密码框整体布局 */
.findPwd-box {
  width: 520px;
  padding: 40px 50px;
  background-color: #fff;
  border: none;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

/* 标题样式 */
.findPwd-box>div:first-child {
  font-size: 28px;
  font-weight: 600;
  text-align: center;
  margin-bottom: 24px;
  color: #333;
}

/* 表单容器 */
.form-findPwd {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 其他样式保持不变 */
.form-input {
  margin-bottom: 0 !important;
}

/* 输入框基础样式 */
:deep(.el-input__wrapper) {
  padding: 1px 16px;
  height: 40px;
  background-color: #FAFAFA;
  border: 1px solid #E8E8E8;
  box-shadow: none !important;
}

/* 处理自动填充的样式 */
:deep(.el-input__inner:-webkit-autofill),
:deep(.el-input__inner:-webkit-autofill:hover),
:deep(.el-input__inner:-webkit-autofill:focus) {
  -webkit-box-shadow: 0 0 0 1000px #FAFAFA inset !important;
  -webkit-text-fill-color: #333 !important;
  transition: background-color 5000s ease-in-out 0s;
}

/* 自动填充时外层容器的样式 */
:deep(.el-input__wrapper.is-focus),
:deep(.el-input__wrapper:has(.el-input__inner:-webkit-autofill)) {
  box-shadow: none !important;
  border-color: #409eff;
  background-color: #FAFAFA;
}

/* 移除输入框的默认聚焦样式 */
:deep(.el-input__inner:focus) {
  outline: none;
  box-shadow: none !important;
}

/* 输入框hover状态 */
:deep(.el-input__wrapper:hover) {
  box-shadow: none !important;
  border-color: #409eff;
}

:deep(.el-space) {
  --el-space-y: 16px !important;
}

.submit-btn {
  width: 100%;
  height: 40px;
  margin-top: 8px;
  font-size: 16px;
  font-weight: 500;
  color: #fff;
  background-color: #409eff;
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background-color: #66b1ff;
}

.submit-btn:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

/* 步骤条样式优化 */
:deep(.el-step__title) {
  font-size: 16px;
  color: #999;
}

:deep(.el-step__title.is-process) {
  color: #409eff;
  font-weight: 500;
}

:deep(.el-step__head.is-process) {
  color: #409eff;
  border-color: #409eff;
}

:deep(.el-step__line) {
  background-color: #E8E8E8;
}

/* 按钮样式 */
:deep(#findPwd-send-sms-code-btn) {
  background-color: #ecf5ff;
  border-color: #d9ecff;
  color: #409eff;
}

:deep(#findPwd-send-sms-code-btn:hover) {
  background-color: #d9ecff;
  color: #66b1ff;
}

:deep(#findPwd-send-sms-code-btn:disabled) {
  background-color: #f4f4f5;
  border-color: #e4e7ed;
  color: #c0c4cc;
}

/* 错误提示信息 */
.msg-wrap {
  width: 100%;
  padding: 8px 16px;
  margin: 4px 0;
  font-size: 14px;
  color: #ff4d4f;
  background-color: #fff2f0;
  border-radius: 6px;
  box-sizing: border-box;
}
</style>
